package com.colin.jipay;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.colin.jipay.activity.MainActivity;
import com.colin.jipay.config.ActConst;
import com.colin.jipay.nxys.helper.NxysHelper;
import com.colin.jipay.starpos.bean.CreateQrcodeBean;
import com.colin.jipay.taobao_hook.TaobaoHook;
import com.colin.jipay.utils.AbSharedUtil;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;
import com.alibaba.fastjson.JSONObject;
import com.colin.jipay.utils.GsonUtils;
import com.colin.jipay.utils.LogUtil;
import com.colin.jipay.utils.MD5;
import com.colin.jipay.utils.PayHelperUtils;

import java.net.URI;
import java.util.HashMap;

import static com.colin.jipay.activity.MainActivity.sendmsg;

/**
 * author    : colin
 * e-mail    : colin12138@gmail.com
 * date      : 2019/6/20 17:41
 * qq        : 1018294974
 */
public class WebService extends WebSocketClient {
    private static URI mUri;
    private static Context mContext;
    private static WebService webService;

    private WebService(URI serverUri) {
        super(serverUri);
        mUri = serverUri;
    }

    public static synchronized WebService getInstance(Context context,URI uri){
        mContext = context;
        if (webService == null){
            webService = new WebService(uri);
        }
        return webService;
    }

    @Override
    public void onOpen(ServerHandshake handshakedata) {
        sendmsg("WS服务已启动");
        HashMap hashMap = new HashMap();
        hashMap.put("type","login");
        hashMap.put("imei","login");
        hashMap.put("Version","login");
        hashMap.put("Model","login");
        hashMap.put("sign","login");
    }

    @Override
    public void onMessage(String message) {
        try{
            JSONObject jsonObject = JSON.parseObject(message);
            if (!"ping".equals(jsonObject.getString("type"))){
                sendmsg(message);
            }
            switch (jsonObject.getString("type")){
                case "ping":
                    //TODO
                    webService.send("{\"type\":\"ping\"}");
                    break;
                case "login":
                    //TODO
                    String clientId = jsonObject.getString("clientid");
                    AbSharedUtil.putString(mContext,"clientid",clientId);
                    String imei = PayHelperUtils.getDevice("IMEI",mContext);
                    String version = PayHelperUtils.getDevice("phoneVersion",mContext);
                    String model = PayHelperUtils.getDevice("phoneModel",mContext);
                    HashMap hashMap = new HashMap();
                    hashMap.put("type","login");
                    hashMap.put("imei",imei);
                    hashMap.put("Version",version);
                    hashMap.put("Model",model);
                    hashMap.put("sign",MD5.md5("login"+AbSharedUtil.getString(mContext,"sign")+imei+clientId+"login"));
                    webService.send(JSON.toJSONString(hashMap));
                    Log.d("client_send==>",JSON.toJSONString(hashMap));
                    break;
                case "getcode":
                    //TODO
                    String type = jsonObject.getString("code_type");
                    JSONObject data = jsonObject;
                    Intent intent = new Intent();
                    switch (type){
                        case "alipaytbs":
                            intent.setAction(ActConst.TAOBAO_PAY);
                            intent.putExtra("itemid",data.getString("itemid"));
                            intent.putExtra("skuid",data.getString("skuid"));
                            AbSharedUtil.putString(mContext,"id",data.getString("id"));
                            AbSharedUtil.putString(mContext,"tid",data.getString("tid"));
                            AbSharedUtil.putString(mContext,"money",data.getString("money"));
                            mContext.sendBroadcast(intent);
                            break;
                            //发送农信易扫的创建二维码的广播，会在农信易扫里面主动调用创建二维码的功能
                            //使用出码支付的功能前，需要先手动在app里面执行一次创建二维码的功能
                        case "nxyspay":
//                            CreateQrcodeBean qrcodeBean= GsonUtils.fromJson(data.toString(),CreateQrcodeBean.class);
                            Intent intent2 = new Intent(NxysHelper.ACTION_CONNECT);
                            String mount = data.getString("money");
                            if (!TextUtils.isEmpty(mount)) {
                                intent2.putExtra("money", mount);
                            }

                            mContext.sendBroadcast(intent2);
                            break;
                        case "unionpay":
                            Intent intent5 = new Intent("com.colin.union.start");
                            intent5.putExtra("money",data.getString("money"));
                            intent5.putExtra("mark",data.getString("mark"));
                            mContext.sendBroadcast(intent5);
                            break;
                        case "alipay":
                            Intent intent6 = new Intent("com.colin.alipay.start");
                            intent6.putExtra("money",data.getString("money"));
                            intent6.putExtra("mark",data.getString("mark"));
                            mContext.sendBroadcast(intent6);
                            break;
                    }

                    break;
                case "1004":
                    //TODO
                    break;
            }
        }catch (Exception e){
            LogUtil.log(e.getMessage());
        }

    }

    @Override
    public void onClose(int code, String reason, boolean remote) {
        sendmsg("WS服务断开,正在尝试重连...");
        new WebService(mUri).connect();
    }

    @Override
    public void onError(Exception ex) {
        sendmsg("WS服务出错"+ex.getMessage());
        //webService.close();
    }
}
